<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <!-- <a-button type="primary" @click="handleForm({})" v-auth="'proj:collect:userCollect:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button> -->
        <a-popconfirm
          title="是否确认取消收藏？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleCancel()"
        >
          <a-button type="danger">
            <Icon icon="ant-design:delete-outlined" /> {{ t('取消收藏') }}
          </a-button>
        </a-popconfirm>
      </template>
      <!-- <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.userCode }}
        </a>
      </template> -->

      <template #projectno="{ record }">
        <!-- <a @click="handleDetailsForm(record)" >
          <span style="text-decoration: underline;margin-right: 5px;">查看</span>
        </a>
        {{ record.projectCode }} -->
        <a @click="handleDetailsForm(record)" >
          {{ record.projectCode }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsProjCollectUserCollectList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    userCollectDelete,
    userCollectListData,
    userCollectBatchDelete,
  } from '/@/api/proj/collect/userCollect';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { userCollectListDataNoPage } from '/@/api/proj/info/pmProjectinformation';

  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();

  const { t } = useI18n('proj.collect.userCollect');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('用户收藏项目管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      // {
      //   label: t('用户编码'),
      //   field: 'userCode',
      //   component: 'Input',
      // },
      
      {
        label: t('项目编码'),
        field: 'projectCode',
        component: 'Input',
      },
      {
        label: t('项目名称'),
        field: 'projectName',
        component: 'Input',
      },
      {
        label: t('用户名称'),
        field: 'userName',
        component: 'Input',
      },
      // {
      //   label: t('关注度'),
      //   field: 'attention',
      //   component: 'Input',
      // },
    ],
  };

  const tableColumns: BasicColumn[] = [
    // {
    //   title: t('用户编码'),
    //   dataIndex: 'userCode',
    //   key: 'a.user_code',
    //   sorter: true,
    //   width: 230,
    //   align: 'left',
    //   slot: 'firstColumn',
    // },
   
    {
      title: t('项目编码'),
      dataIndex: 'projectCode',
      key: 'a.project_code',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'projectno',
    },
    {
      title: t('项目名称'),
      dataIndex: 'projectName',
      key: 'a.project_name',
      sorter: true,
      width: 220,
      align: 'left',
    },
    {
      title: t('用户名称'),
      dataIndex: 'userName',
      key: 'a.user_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    // {
    //   title: t('关注度'),
    //   dataIndex: 'attention',
    //   key: 'a.attention',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    // },
  ];

  const actionColumn: BasicColumn = {
    width: 130,
    actions: (record: Recordable) => [
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('取消收藏'),
        popConfirm: {
          title: t('是否确认取消收藏'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: userCollectListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleDetailsForm(record: Recordable) {
    router.push({
      path: '/proj/info/pmProjectinformation/detailsForm',
      query: {
        id: record.projInfo.id,
        code: record.projectCode,
        tabTitle: `项目详情(${record.projectCode})`,
      },
    });
  }

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await userCollectDelete(record);
    showMessage(res.message);
    handleSuccess();

    let res1 = await userCollectListDataNoPage();
    await userStore.setProjecteList(res1);
  }

  // 取消收藏
  async function handleCancel() {
    if (getSelectRows().length) {
      let selIds = '';
      getSelectRows().forEach((item) => {
        selIds += item.id + ',';
      });
      const res = await userCollectBatchDelete({ selIds: selIds });
      showMessage(res.message);
      handleSuccess();
      let res1 = await userCollectListDataNoPage();
      await userStore.setProjecteList(res1);
    } else {
      showMessage('请选择一行数据！');
    }
  }

  function handleSuccess() {
    reload();
  }
</script>
